layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','element','util','upload','baseConfig'],function(){

    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        baseConfig = layui.baseConfig,
        $ = layui.jquery;

    //获取海关信息
    getDataDefine( $, "CUSTOMHOUSE", function( data){
        var sel = $( "select[name=customHouse]");
        var customerHouses = data;
        for( var j = 0,len = customerHouses.length; j < len; j++){
            var customerHouse = customerHouses[j];
            $(sel).append( "<option value=\"" + customerHouse.dataValue + "\">" + customerHouse.dataKey + "</option>");
        }
        form.render('select');
    });

    //上传发票
    form.on("submit(uploadInvoiceBtn)",function( data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        var invoice = data.field.invoice;
        var invoiceRate = data.field.invoiceRate;
        var invoiceMoney = data.field.invoiceMoney;
        var checkLimit = parseFloat( $("input[name=montantTTC]").val()/6).toFixed( 2);
        if( !invoice){
            top.layer.alert( "请上传发票");
            return;
        }
        if( invoiceMoney.indexOf( "e") != -1){
            top.layer.alert( "TVA只能输入数字，请重新输入");
            return;
        }
        if( invoiceRate == "other" && ( invoiceMoney < 0 || invoiceMoney > checkLimit)){
            top.layer.alert( "TVA数值在0-" + checkLimit + "之间，请重新输入");
            return;
        }

        //提交数据
        $.post("/qonto/uploadInvoice",{
            id : data.field.id,
            invoice : invoice,
            invoiceImg : data.field.invoiceImg,
            invoiceRate : invoiceRate,
            invoiceMoney : data.field.invoiceMoney
        },function( res){
            if( res.code == 200){
                setTimeout(function(){
                    top.layer.close( index);
                    top.layer.msg( "上传发票成功");
                    layer.closeAll( "iframe");
                    //刷新父页面
                    parent.location.reload();
                },500);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });

    //审核发票
    form.on("submit(invoiceReviewBtn)",function( data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        if( data.field.invoiceStatus == "PASS"){
            if( !data.field.customHouse){
                top.layer.alert( "请选择海关");
                return
            }
        }
        //提交数据
        $.post("/qonto/reviewInvoice",{
            id : data.field.id,
            invoiceStatus : data.field.invoiceStatus,
            invoiceMemo : data.field.invoiceMemo,
            customHouse : data.field.customHouse
        },function( res){
            if( res.code == 200){
                setTimeout(function(){
                    top.layer.close( index);
                    top.layer.msg( "审核成功");
                    layer.closeAll( "iframe");
                    //刷新父页面
                    parent.location.reload();
                },500);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });

    //审核发票,并自动跳转到下一张
    form.on("submit(invoiceReviewBtnAndNext)",function( data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        if( data.field.invoiceStatus == "PASS"){
            if( !data.field.customHouse){
                top.layer.alert( "请选择海关");
                return
            }
        }
        //提交数据
        $.post("/qonto/reviewInvoice",{
            id : data.field.id,
            invoiceStatus : data.field.invoiceStatus,
            invoiceMemo : data.field.invoiceMemo,
            customHouse : data.field.customHouse
        },function( res){
            if( res.code == 200){
                top.layer.close( index);
                //查询下一条待审核的数据
                $.post("/qonto/getLastById",{
                    id : data.field.id,
                    invoiceStatus : data.field.invoiceStatus,
                    invoiceMemo : data.field.invoiceMemo,
                    customHouse : data.field.customHouse
                },function( res){
                    var data = res.data;
                    if( data){
                        top.layer.msg( "审核成功，自动获取下一条待审核的记录");
                        $( "input[name=id]").val( data.id).attr( "readonly", true);//标识是修改还是新增
                        if( data.invoice){
                            $( "input[name=invoice]").val( data.invoice).attr( "readonly", false);
                            $( "input[name=invoiceImg]").val( data.invoiceImg).attr( "readonly", false);
                            $( "#pdfImg").attr( "src", data.invoiceImg);
                        }
                        $( "input[name=dateDeDepense]").val( data.dateDeDepense).attr( "readonly", true);
                        $( "input[name=fournisseur]").val( data.fournisseur).attr( "readonly", true);
                        if( data.invoiceRate == "other"){
                            $( "#invoiceRateRadioThree").attr( "checked", "checked").attr( "readonly", true);
                        }else if( data.invoiceRate == "20%"){
                            $( "#invoiceRateRadioOne").attr( "checked", "checked").attr( "readonly", true);
                        }else{
                            $( "#invoiceRateRadioTwo").attr( "checked", "checked").attr( "readonly", true);
                        }
                        $( "input[name=montantTTC]").val( parseFloat( data.montantTTC).toFixed( 2)).attr( "readonly", true);
                        $( "input[name=invoiceMoney]").val( parseFloat( data.invoiceMoney).toFixed( 2)).attr( "readonly", true);
                        form.render();
                    }else{
                        top.layer.msg( "审核成功");
                        layer.closeAll( "iframe");
                        //刷新父页面
                        parent.location.reload();
                    }
                });
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });

    form.on('radio(invoiceRateRadio)', function(data){
        if( data.value == "other"){
            $("input[name=invoiceMoney]").removeAttr( "readonly");
        }else if( data.value == "0"){
            $("input[name=invoiceMoney]").val( "0").attr( "readonly", "readonly");
        }else if( data.value == "20%"){
            $("input[name=invoiceMoney]").val( (parseFloat( $("input[name=montantTTC]").val())/5).toFixed( 2));
        }
    });

    //上传发票
    upload.render({
        elem: '.invoiceBtn',
        url: '/file/upload?type=invoice',
        method : "post",
        accept: "file",
        exts: "pdf",
        done: function( res){
            if( res.code == 200){
                $("input[name=invoice]").val( res.data.pdfFilePath);
                $("input[name=invoiceImg]").val( res.data.imgFilePath);
                $("#pdfImg").attr( "src", res.data.imgFilePath);
            }
        }
    });

    //删除发票
    $(".deleteInvoice").bind("click",function(){
        $("#pdfImg").attr( "src", "");
        $("input[name=invoiceImg]").val( "");
        $("input[name=invoice]").val( "");
    });

    //发票丢失。按下这个按钮后，TVA% 处自动选择SANS TVA. 不需要上传发票也能确定这个页面。以前是必须要上传发票才能确定。如果再按这个按钮，按钮弹起，TVA% 处回复到默认20%
    $(".facturePerdu").bind("click",function(){
        var facturePerdu = $( "input[name=facturePerdu]").val();
        //第一次点击
        if( facturePerdu == "0"){
            $("#invoiceRateRadioTwo").prop('checked', true);
            $("input[name=facturePerdu]").val( "1"); //这个1代表发票丢失，不需要上传发票
            $("input[name=invoice]").val( "LOSE");
            $("input[name=invoiceImg]").val( "LOSE");
        }else{//第二次点击   TVA% 处回复到默认20%
            $("#invoiceRateRadioOne").prop('checked', true);
            $("input[name=facturePerdu]").val( "0");
            $("input[name=invoice]").val( "");
            $("input[name=invoiceImg]").val( "");
        }
        form.render();
    });
});
